<template>
  <el-dialog
    title="提示"
    :visible="dialogVisible"
    width="50%"
    @close="closeFn"
  >
    <div class="login-container">
      <!-- 编辑 -->
      <el-form v-if="userInfo.id" ref="userForm" :model="userInfo" :rules="rules" auto-complete="off" label-position="left">
        <el-form-item prop="username">
          <span class="svg-container">
            <svg-icon icon-class="user" />
          </span>
          <el-input
            ref="username"
            v-model="userInfo.username"
            placeholder="请输入用户名"
            name="username"
            type="text"
            tabindex="1"
            auto-complete="off"
          />
        </el-form-item>

        <el-form-item prop="email">
          <span class="svg-container">
            <svg-icon icon-class="email" />
          </span>
          <el-input
            ref="email"
            v-model="userInfo.email"
            placeholder="请输入邮箱"
            name="email"
            type="text"
            tabindex="1"
            auto-complete="off"
          />
        </el-form-item>

        <el-form-item prop="password">
          <span class="svg-container">
            <svg-icon icon-class="password" />
          </span>
          <el-input
            :key="passwordType"
            ref="password"
            v-model="userInfo.password"
            :type="passwordType"
            placeholder="请输入密码"
            name="password"
            tabindex="2"
            auto-complete="off"
          />
          <span class="show-pwd" @click="showPwd(1)">
            <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
          </span>
        </el-form-item>
        <el-form-item label="员工头像">
          <!-- 放置上传图片--加ref属性--以便用于图片回显 -->
          <image-uploader ref="picture" />
        </el-form-item>
      </el-form>
      <!-- 新增 -->
      <el-form v-else ref="registerForm" :model="registerForm" :rules="rules" auto-complete="off" label-position="left">
        <el-form-item prop="username">
          <span class="svg-container">
            <svg-icon icon-class="user" />
          </span>
          <el-input
            ref="username"
            v-model="registerForm.username"
            placeholder="请输入用户名"
            name="username"
            type="text"
            tabindex="1"
            auto-complete="off"
          />
        </el-form-item>

        <el-form-item prop="email">
          <span class="svg-container">
            <svg-icon icon-class="email" />
          </span>
          <el-input
            ref="email"
            v-model="registerForm.email"
            placeholder="请输入邮箱"
            name="email"
            type="text"
            tabindex="1"
            auto-complete="off"
          />
        </el-form-item>

        <el-form-item prop="password">
          <span class="svg-container">
            <svg-icon icon-class="password" />
          </span>
          <el-input
            :key="passwordType"
            ref="password"
            v-model="registerForm.password"
            :type="passwordType"
            placeholder="请输入密码"
            name="password"
            tabindex="2"
            auto-complete="off"
          />
          <span class="show-pwd" @click="showPwd(1)">
            <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
          </span>
        </el-form-item>

        <el-form-item prop="checkPass">
          <span class="svg-container">
            <svg-icon icon-class="password" />
          </span>
          <el-input
            :key="passwordType"
            ref="checkPass"
            v-model="registerForm.checkPass"
            :type="passwordType"
            placeholder="请确认密码"
            name="password"
            tabindex="2"
            auto-complete="off"
          />
          <span class="show-pwd" @click="showPwd(2)">
            <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
          </span>
        </el-form-item>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button @click="closeFn">取 消</el-button>
      <el-button type="primary" @click="handle">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { getUerInfoById, editUser } from '@/api/employees'
export default {
  props: {
    dialogVisible: {
      type: Boolean,
      require: true
    }
  },
  data() {
    // 二次密码校验
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.registerForm.password) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      registerForm: {
        username: '',
        email: '',
        password: '',
        checkPass: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 3, max: 12, message: '密码长度3到12位', trigger: 'blur' }
        ],
        checkPass: [
          { validator: validatePass2, trigger: 'blur' }
        ],
        email: [
          { required: true, message: '请输入邮箱地址', trigger: 'blur' },
          { type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur'] }
        ]
      },
      userInfo: {},
      passwordType: 'password'
    }
  },
  methods: {
    // 关闭弹窗--清空表单
    closeFn() {
      this.registerForm = {
        username: '',
        email: '',
        password: '',
        checkPass: ''
      }
      this.userInfo = {}
      this.$emit('update:dialogVisible', false)
    },
    // 显示隐藏注册表单的密码
    showPwd(num) {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      if (num === 1) {
        this.$nextTick(() => {
          this.$refs.password.focus()
        })
      } else if (num === 2) {
        this.$nextTick(() => {
          this.$refs.checkPass.focus()
        })
      }
    },
    // 新建用户---和注册同样接口
    async handle() {
      if (this.userInfo.id) {
        await this.$refs.userForm.validate()
        this.userInfo.image = this.$refs.picture.fileList[0].url
        await editUser(this.userInfo)
      } else {
        await this.$refs.registerForm.validate()
        await this.$store.dispatch('user/vuxRegister', this.registerForm)
      }
      this.$emit('refresh')
      this.closeFn()
    },
    // 根据ID获取指定用户信息
    async loadUserInfoById(id) {
      const res = await getUerInfoById(id)
      this.userInfo = res.data
      if (this.userInfo.image) {
        this.$nextTick(() => {
          this.$refs.picture.fileList.push({
            url: this.userInfo.image
          })
        })
      }
    }
  }
}
</script>

<style lang="scss">
$bg:#fff;
$light_gray:black;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
$bg:#fff;
$dark_gray:#fff;
$light_gray:#eee;

.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;

//   .login-form {
//     position: relative;
//     width: 520px;
//     max-width: 100%;
//     padding: 160px 35px 0;
//     margin: 0 auto;
//     overflow: hidden;
//   }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>
